package com.iflytek.jcy.supervise.clue.service.impl;

import com.iflytek.jcy.supervise.clue.common.enums.CluePushStatusEnum;
import com.iflytek.jcy.supervise.clue.dto.QueryClueResultDTO;
import com.iflytek.jcy.supervise.clue.dto.response.TaskDetailDTO;
import com.iflytek.jcy.supervise.clue.entity.Clue;
import com.iflytek.jcy.supervise.clue.entity.ClueCollectionMap;
import com.iflytek.jcy.supervise.clue.entity.CluePushDetail;
import com.iflytek.jcy.supervise.clue.entity.TaskDetail;
import com.iflytek.jcy.supervise.clue.mapper.ClueCollectionMapMapper;
import com.iflytek.jcy.supervise.clue.mapper.ClueMapper;
import com.iflytek.jcy.supervise.clue.mapper.CluePushDetailMapper;
import com.iflytek.jcy.supervise.clue.mapper.TaskDetailMapper;
import com.iflytek.jcy.supervise.clue.service.ITaskDetailService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 任务详情表 服务实现类
 * </p>
 *
 * @author dgyu
 * @since 2022-10-20 */
@Service
public class TaskDetailServiceImpl extends ServiceImpl<TaskDetailMapper, TaskDetail> implements ITaskDetailService {

	@Autowired
	private TaskDetailMapper		taskDetailMapper;
	@Autowired
	private ClueMapper				clueMapper;
	@Autowired
	private ClueCollectionMapMapper	clueCollectionMapMapper;
	@Autowired
	private CluePushDetailMapper	cluePushDetailMapper;

	@Override
	public IPage<TaskDetailDTO> listResult(QueryClueResultDTO queryClueResultDTO) {
		Page<TaskDetail> page = new Page<>(queryClueResultDTO.getPageNo(), queryClueResultDTO.getPageSize());
		LambdaQueryWrapper<TaskDetail> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(TaskDetail::getIsDelete, 0);
		wrapper.eq(queryClueResultDTO.getClueId() != null && queryClueResultDTO.getClueId() > 0, TaskDetail::getClueId, queryClueResultDTO.getClueId());
		wrapper.orderByAsc(TaskDetail::getSupervisionObject).orderByAsc(TaskDetail::getSupervisionObjectName).orderByAsc(TaskDetail::getSupervisionPointName).orderByDesc(TaskDetail::getCreateTime);
		IPage<TaskDetail> pageClue = this.baseMapper.selectPage(page, wrapper);
		IPage<TaskDetailDTO> pageDtoClue = new Page<>();
		List<TaskDetail> list = pageClue.getRecords();
		// 查询改线索是否已经被推送
		List<Integer> detailsIds = Optional.ofNullable(list).map(Collection::stream).map(stream -> stream.map(TaskDetail::getId).collect(Collectors.toList()))
		        .orElse(new ArrayList<>(Arrays.asList(0)));
		LambdaQueryWrapper<CluePushDetail> cluePushDetailWrapper = new LambdaQueryWrapper<>();
		cluePushDetailWrapper.in(CluePushDetail::getDetailId, detailsIds);
		List<CluePushDetail> pushDetails = this.cluePushDetailMapper.selectList(cluePushDetailWrapper);
		Set<Integer> set = Optional.ofNullable(pushDetails).map(Collection::stream).map(stream -> stream.map(CluePushDetail::getDetailId).collect(Collectors.toSet())).orElse(new HashSet<>());

		List<TaskDetailDTO> dtoList = new ArrayList<>();
		BeanUtils.copyProperties(pageClue, pageDtoClue);
		// 查询推送状态
		Clue clue = this.clueMapper.selectById(queryClueResultDTO.getClueId());
		Integer cluePushStatus = clue.getCluePushStatus();
		list.forEach(v -> {
			Integer id = v.getId();
			TaskDetailDTO taskDetailDTO = new TaskDetailDTO();
			BeanUtils.copyProperties(v, taskDetailDTO);
			taskDetailDTO.setCluePushStatus(cluePushStatus);
			// 线索集被推送或者线索自己被推送 则不能删除 不显示删除按钮
			if (set.contains(id) || cluePushStatus.equals(CluePushStatusEnum.PUSHED.getCode())) {
				taskDetailDTO.setCanDelete(false);
			}
			dtoList.add(taskDetailDTO);
		});
		pageDtoClue.setRecords(dtoList);
		return pageDtoClue;
	}

	@Override
	public Integer setWeight(Integer id, Integer weight) {
		LambdaUpdateWrapper<TaskDetail> wrapper = new LambdaUpdateWrapper<TaskDetail>().set(TaskDetail::getWeight, weight).set(TaskDetail::getUpdateTime, new Date()).eq(TaskDetail::getId, id);
		int rows = taskDetailMapper.update(null, wrapper);
		return rows;
	}

	@Override
	public Integer deleteById(Integer id) {
		TaskDetail taskDetail = this.taskDetailMapper.selectById(id);
		LambdaUpdateWrapper<TaskDetail> wrapper = new LambdaUpdateWrapper<>();
		wrapper.set(TaskDetail::getIsDelete, 1);
		wrapper.set(TaskDetail::getUpdateTime, new Date());
		wrapper.eq(TaskDetail::getId, id);
		int rows = taskDetailMapper.update(null, wrapper);
		if (rows > 0) {
			Integer clueId = Optional.ofNullable(taskDetail).map(v -> v.getClueId()).orElse(0);

			LambdaQueryWrapper<TaskDetail> wrapper2 = new LambdaQueryWrapper<TaskDetail>();
			wrapper2.eq(TaskDetail::getClueId, clueId);
			wrapper2.eq(TaskDetail::getIsDelete, 0);

			Long clueNum = taskDetailMapper.selectCount(wrapper2);
			// 更新线索数数量-1
			LambdaUpdateWrapper<Clue> wrapper1 = new LambdaUpdateWrapper<>();
			wrapper1.set(Clue::getClueNum, clueNum);
			wrapper1.set(Clue::getUpdateTime, new Date());
			wrapper1.eq(Clue::getId, clueId);
			this.clueMapper.update(null, wrapper1);

			// 删除线索集线索关系表
			LambdaUpdateWrapper<ClueCollectionMap> wrapper3 = new LambdaUpdateWrapper<>();
			wrapper3.eq(ClueCollectionMap::getTaskDetailId, id);
			this.clueCollectionMapMapper.delete(wrapper3);
		}
		return rows;
	}

	@Override
	public Integer feedback(JSONObject jSONObject) {
		Integer id = jSONObject.getInteger("id");
		Integer evaluationResult = jSONObject.getInteger("evaluationResult");
		String bmsah = jSONObject.getString("bmsah");
		String ajmc = jSONObject.getString("ajmc");
		String info = jSONObject.getString("info");
		LambdaUpdateWrapper<TaskDetail> wrapper = new LambdaUpdateWrapper<>();
		if (evaluationResult != null && evaluationResult >= 0) {
			wrapper.set(TaskDetail::getEvaluationResult, evaluationResult);
		}
		if (StringUtils.isNotEmpty(bmsah)) {
			wrapper.set(TaskDetail::getBmsah, bmsah);
		}
		if (StringUtils.isNotEmpty(ajmc)) {
			wrapper.set(TaskDetail::getAjmc, ajmc);
		}
		if (StringUtils.isNotEmpty(info)) {
			wrapper.set(TaskDetail::getInfo, info);
		}
		wrapper.set(TaskDetail::getUpdateTime, new Date());
		wrapper.eq(TaskDetail::getId, id);
		int rows = taskDetailMapper.update(null, wrapper);
		return rows;
	}
}
